var aceEditor;
var aceEditorFieldName;
function initScriptCtl(fieldName) {
  aceEditorFieldName = fieldName;
  aceEditor = ace.edit("pre_" + fieldName);
  aceEditor.setTheme("ace/theme/eclipse");
  // aceEditor.setTheme("ace/theme/terminal");
  aceEditor.getSession().setMode("ace/mode/java");
    
  setTimeout(function() {
    aceEditor.setOptions({
      readOnly: false,
      highlightActiveLine: true,
      highlightGutterLine: true
    })
  }, 500);

  // 会使得光标不显示
	// aceEditor.renderer.$cursorLayer.element.style.opacity=0;   
 
	aceEditor.setValue($(fo(fieldName)).val());	
}

// ajaxForm序列化提交数据之前的回调函数
function ctlOnBeforeSerialize(formName) {
	console.log('ctlOnBeforeSerialize fo(aceEditorFieldName, formName)', fo(aceEditorFieldName, formName));
	// ajaxForm提交前，必须先提取赋值
	$(fo(aceEditorFieldName, formName)).val(aceEditor.getValue());
}

function getScript() {
	return aceEditor.getValue();
}

function setScript(script) {
	aceEditor.setValue(script);
}

// var ideUrl = "<%=url%>";
var ideWin;
var cwsToken = "";

function openIdeWin() {
	// ideWin = openWinMax(ideUrl);
	openScriptDesigner(getScript());
}

var onMessage = function(e) {
	var d = e.data;
	var data = d.data;
	var type = d.type;
	if (type=="setScript") {
		setScript(data);
		if (d.cwsToken!=null) {
			cwsToken = d.cwsToken;
			// ideUrl = "<%=request.getContextPath()%>/admin/script_frame.jsp?cwsToken=" + cwsToken;
		}
	}
	else if (type=="getScript") {
		var data={
		    "type":"openerScript",
		    // "version":"<%=version%>",
		    // "spVersion":"<%=spVersion%>",
		    "scene": "ScriptCtl",		    
		    "data":getScript()
	    }
		ideWin.leftFrame.postMessage(data, '*');
	} else if (type == "setCwsToken") {
    cwsToken = d.cwsToken;
		// ideUrl = "<%=request.getContextPath()%>/admin/script_frame.jsp?cwsToken=" + cwsToken;
	}
}